<?php
class Notepad_History {
	const SEPARATOR = "\n";
	protected $settings = null;
	protected $file = null;
    protected $files = array();
    protected $menu = null;
    public function __construct(Notepad_Settings $settings) {
        $this->settings = $settings;
        $this->file = Kernel::instance()->getPath() . 'history.dat';
        //$this->file = PROJECT_DATA_DIR . $settings->get('history.file');
        $this->load();
    }
    public function __destruct() {
    	$this->save();
    }
    public function load() {
		if(is_file($this->file)) {
        	$this->files = explode(self::SEPARATOR, file_get_contents($this->file));
	        if(!current($this->files))
    	        array_shift($this->files);
		}
    }
    public function add($path) {
    	if(is_array($path)) {
    		foreach($path as $v)
    			$this->add($v);
    		return true;
    	}

    	if(($key = array_search($path, $this->files)) !== false)
            unset($this->files[$key]);

		$this->files[] = $path;
        if(count($this->files) > 30)
        	$this->files = array_slice($this->files, 0, 30);
        $this->save();
        return true;
    }
    public function get() {
    	return $this->files;
    }
    public function getMenu() {
    	return $this->menu;
    }
    public function setMenu(GtkMenu $menu) {
    	$this->menu = $menu;
    }
    public function save() {
		$this->files = array_values($this->files);
    	@file_put_contents($this->file, implode(self::SEPARATOR, $this->files));
    }
}
?>
